﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NRails.Demo.Models;
using NRails.Macros;
using Nemerle.Extensions;

namespace NRails.Demo.Controllers
{
    [HandleError]
    public class HomeController : AppController
    {
        public Index() : ActionResult
        {
            def message = $"NRails env: '$(db.Env)'. We have $(db.Persons.Count()) persons.";
            def taxonomies =  db.Doctors.Select(d => new (text = d.Taxonomy, doctor = d.PersonID)).ToList();

            View(viewmodel ( message, taxonomies ));
        }

        public Doctor(id : int) : ActionResult
        {
            def doctor =  db.Doctors.Where(d => d.PersonID == id)
                .Select(d => new (
                    taxonomy = d.Taxonomy, 
                    name = new (
                        LastName = d.Person.LastName, 
                        MiddleName = d.Person.MiddleName, 
                        FirstName = d.Person.FirstName
                    )
                )).Single();
            
            View(viewmodel (doctor));
        }

        public About() : ActionResult
        {
            View(viewmodel ( description = "NRails demo project", time = DateTime.Now ))
        }
    }
}
